package com.btjava.maven.pomver.handler;

import com.btjava.maven.pomver.data.CommonData;
import com.btjava.maven.pomver.utils.XmlParseUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Element;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class JarRepositorieshandler {
    /**
     * 获取当前项目的maven配置
     * @return maven配置路径
     */
    public static List<String> getMavenRepositoryUrl(){
        String filePath =CommonData.project.getBaseDir().getPath()+ File.separator+".idea"+File.separator+"jarRepositories.xml";
        if(!(new File(filePath).exists())){
            return null;
        }
        List<Element> elementList = XmlParseUtils.getElementByNamePath(filePath,"[name=RemoteRepositoriesConfiguration]>remote-repository>[name=url]");
        if(Objects.isNull(elementList) || elementList.isEmpty()){
            return null;
        }
        List<String> urlList = new ArrayList<>();
        for(Element e: elementList){
            String url =e.attributeValue("value");
            if(StringUtils.isBlank(url)){
                continue;
            }
            url = SettingXmlHandler.parseUrl(url);
            urlList.add(url);
        }
        return urlList;
    }
}
